//
//  CJExten+Date.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-06-13.
//

import Foundation

public extension Date {
    
    /// 时间戳(秒)
    static var cj_stampSecond: String {
        let timeInterval = Date().timeIntervalSince1970
        return "\(timeInterval)"
    }
    
    /// 时间戳(毫秒)
    static var cj_stampMillisecond: String {
        let timeInterval = Date().timeIntervalSince1970 * 1000
        return "\(timeInterval)"
    }
    
    /// 时钟戳
    static var cj_stampFormat: String {
        let sf = Date().cj_dateString(format: "YYYYMMddHHmmssSSS")
        return sf
    }
    
    /// stamp转DateFormat字符串 format默认 "YYYY-MM-dd HH:mm:ss"
    static func cj_dateString(stamp: Double, format: String = "YYYY-MM-dd HH:mm:ss", timeZone: TimeZone? = nil, locale: Locale? = nil) -> String {
        let date = Date(timeIntervalSince1970: stamp)
        let dateFormat = DateFormatter()
        dateFormat.dateFormat = format
        if let timeZone = timeZone {
            dateFormat.timeZone = timeZone
        }
        if let locale = locale {
            dateFormat.locale = locale
        }
        let dateString = dateFormat.string(from: date)
        return dateString
    }
    
}

public extension Date {
    
    /// 转DateFormat字符串 format默认 "YYYY-MM-dd HH:mm:ss"
    func cj_dateString(format: String = "YYYY-MM-dd HH:mm:ss") -> String {
        let dateFormat = DateFormatter()
        dateFormat.dateFormat = format
        let dateString = dateFormat.string(from: self)
        return dateString
    }
    
    /// 转DateFormat字符串 format默认 "YYYY-MM-dd HH:mm:ss:SSS"
    func cj_dateZoneString(format: String = "YYYY-MM-dd HH:mm:ss:SSS", timeZone: TimeZone = .current, locale: Locale = .current) -> String {
        let dateFormat = DateFormatter()
        dateFormat.dateFormat = format
        dateFormat.timeZone = timeZone
        dateFormat.locale = locale
        let dateString = dateFormat.string(from: self)
        return dateString
    }
}

public extension Date {
    
    /// 年
    var cj_year: Int {
        return NSCalendar.current.component(.year, from: self)
    }
    
    /// 月
    var cj_month: Int {
        return NSCalendar.current.component(.month, from: self)
    }
    
    /// 日
    var cj_day: Int {
        return NSCalendar.current.component(.day, from: self)
    }
    
    /// 时
    var cj_hour: Int {
        return NSCalendar.current.component(.hour, from: self)
    }
    
    /// 分
    var cj_minute: Int {
        return NSCalendar.current.component(.minute, from: self)
    }
    
    /// 秒
    var cj_second: Int {
        return NSCalendar.current.component(.second, from: self)
    }
    
    /// 纳秒
    var cj_nanosecond: Int {
        return NSCalendar.current.component(.nanosecond, from: self)
    }
    
    /// 星期(星期日:1 星期一:2 ...)
    var cj_weekday: Int {
        return NSCalendar.current.component(.weekday, from: self)
    }
    
    /// 周(月)
    var cj_weekdayOrdinal: Int {
        return NSCalendar.current.component(.weekdayOrdinal, from: self)
    }
    
    /// 月周
    var cj_weekOfMonth: Int {
        return NSCalendar.current.component(.weekOfMonth, from: self)
    }
    
    /// 年周
    var cj_weekOfYear: Int {
        return NSCalendar.current.component(.weekOfYear, from: self)
    }
    
    /// 季
    var cj_quarter: Int {
        return NSCalendar.current.component(.quarter, from: self)
    }
    
    /// 是否闰月
    var cj_isLeapMonth: Bool {
        return NSCalendar.current.dateComponents([.month], from: self).isLeapMonth ?? false
    }
    
    /// 是否闰年
    var cj_isLeapYear: Bool {
        let year = cj_year
        let isLeapYear = ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0)))
        return isLeapYear
    }
    
    /// 是否今天
    var cj_today: Bool {
        if (fabs(timeIntervalSinceNow) >= 60 * 60 * 24) {
            return false
        }
        return Date().cj_day == cj_day
    }
}

public extension Date {
    
    /// 加年
    func cj_dateAddYears(_ value: Int) -> Date {
        let cale = Calendar.current
        var comp = DateComponents()
        comp.setValue(value, for: .year)
        let date = cale.date(byAdding: comp, to: self) ?? self
        return date
    }
    
    /// 加月
    func cj_dateAddMonths(_ value: Int) -> Date {
        let cale = Calendar.current
        var comp = DateComponents()
        comp.setValue(value, for: .month)
        let date = cale.date(byAdding: comp, to: self) ?? self
        return date
    }
    
    /// 加周
    func cj_dateAddWeeks(_ value: Int) -> Date {
        let cale = Calendar.current
        var comp = DateComponents()
        comp.setValue(value, for: .weekdayOrdinal)
        let date = cale.date(byAdding: comp, to: self) ?? self
        return date
    }
    
    /// 加天
    func cj_dateAddDays(_ value: Double) -> Date {
        let timeInt = timeIntervalSinceReferenceDate + 86400 * value
        let date = Date(timeIntervalSinceReferenceDate: timeInt)
        return date
    }
    
    /// 加时
    func cj_dateAddHours(_ value: Double) -> Date {
        let timeInt = timeIntervalSinceReferenceDate + 3600 * value
        let date = Date(timeIntervalSinceReferenceDate: timeInt)
        return date
    }
    
    /// 加分
    func cj_dateAddMinutes(_ value: Double) -> Date {
        let timeInt = timeIntervalSinceReferenceDate + 60 * value
        let date = Date(timeIntervalSinceReferenceDate: timeInt)
        return date
    }
    
    /// 加秒
    func cj_dateAddSeconds(_ value: Double) -> Date {
        let timeInt = timeIntervalSinceReferenceDate + value
        let date = Date(timeIntervalSinceReferenceDate: timeInt)
        return date
    }
    
}
